<template>
  <page-header-wrapper>
    <a-card
      :bordered="true"
      :hoverable="true">
      <!-- 条件搜索 -->
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="城市名称" prop="cityName">
                <a-input v-model="queryParam.cityName" allow-clear placeholder="请输入城市名称" />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="城市级别" prop="cityLevel">
                <a-select v-model="queryParam.cityLevel" placeholder="请选择城市线级" style="width: 100%">
                  <a-select-option v-for="(d, index) in cityLevelOptions" :key="index" :value="d.dictValue">
                    {{ d.dictLabel }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="8" :sm="24">
                <a-form-item label="城市线级" prop="linesLevel">
                  <a-select v-model="queryParam.linesLevel" placeholder="请选择城市线级" style="width: 100%">
                    <a-select-option v-for="(d, index) in linesLevelOptions" :key="index" :value="d.dictValue">
                      {{ d.dictLabel }}
                    </a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
            </template>
            <a-col :md="!advanced && 8 || 24" :sm="24">
              <span
                :style="advanced && { float: 'right', overflow: 'hidden' } || {} "
                class="table-page-search-submitButtons">
                <a-button type="primary" @click="handleQuery"><a-icon type="search" />查询</a-button>
                <a-button style="margin-left: 8px" @click="resetQuery"><a-icon type="redo" />重置</a-button>
                <a style="margin-left: 8px" @click="toggleAdvanced">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'" />
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <!-- 操作 -->
      <div class="table-operations">
        <a-button v-hasPermi="['platform:city:add']" type="primary" @click="$refs.createForm.handleAdd()">
          <a-icon type="plus" />
          新增
        </a-button>
        <a-button
          v-hasPermi="['platform:city:edit']"
          :disabled="single"
          type="primary"
          @click="$refs.createForm.handleUpdate(undefined, ids)">
          <a-icon type="edit" />
          修改
        </a-button>
        <a-button v-hasPermi="['platform:city:remove']" :disabled="multiple" type="danger" @click="handleDelete">
          <a-icon type="delete" />
          删除
        </a-button>
        <a-button
          :loading="loading"
          :style="{float: 'right'}"
          icon="reload"
          shape="circle"
          type="dashed"
          @click="getList" />
      </div>
      <!-- 增加修改 -->
      <create-form
        ref="createForm"
        :cityLevelOptions="cityLevelOptions"
        :linesLevelOptions="linesLevelOptions"
        @ok="getList"
      />
      <!-- 数据详情 -->
      <view-form
        ref="viewForm"
        :cityLevelOptions="cityLevelOptions"
        :linesLevelOptions="linesLevelOptions"
      />
      <!-- 数据展示 -->
      <a-table
        :columns="columns"
        :data-source="list"
        :expandedRowKeys="expandedKeys"
        @expand="onExpandCurrent"
        :loading="loading"
        :pagination="false"
        :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        :bordered="true"
        :size="tableSize"
        rowKey="cityId">
        <span
          slot="expandedRowRender"
          slot-scope="text"
        >
          <area-data
            ref="areaData"
            :parentId="text.cityId"
          />
        </span>

        <template slot="cityName" slot-scope="text, record">
          <a
            v-if="checkPermi(['platform:city:query'])"
            @click="$refs.viewForm.handleView(record, undefined)">{{ record.cityName }}</a>
          <span v-else>{{ record.cityName }}</span>
        </template>
        <span slot="cityLevel" slot-scope="text, record">
          {{ cityLevelFormat(record.cityLevel) }}
        </span>
        <span slot="linesLevel" slot-scope="text, record">
          {{ linesLevelFormat(record.linesLevel) }}
        </span>

        <span slot="operation" slot-scope="text, record">
          <a v-hasPermi="['platform:city:edit']" @click="$refs.createForm.handleUpdate(record, undefined)">
            修改
          </a>
          <a-divider v-hasPermi="['platform:city:remove']" type="vertical" />
          <a v-hasPermi="['platform:city:remove']" style="color:red" @click="handleDelete(record)">
            删除
          </a>
        </span>
      </a-table>
      <!-- 分页 -->
      <a-pagination
        :current="queryParam.pageNum"
        :page-size="queryParam.pageSize"
        :showTotal="total => `共 ${total} 条`"
        :total="total"
        class="ant-table-pagination"
        show-quick-jumper
        show-size-changer
        @change="changeSize"
        @showSizeChange="onShowSizeChange"
      />
    </a-card>
  </page-header-wrapper>
</template>

<script>

import { listCity, delCity } from '@/api/platform/city'
import { checkPermi } from '@/utils/permission'
import CreateForm from './modules/CreateForm'
import ViewForm from './modules/ViewForm'
import AreaData from './modules/AreaData'
import CreateDataForm from './modules/CreateDataForm'

export default {
  name: 'City',
  components: {
    CreateForm,
    ViewForm,
    AreaData,
    CreateDataForm
  },
  data () {
    return {
      // 城市名称字典
      cityLevelOptions: [],
      // 城市线级字典
      linesLevelOptions: [],
      // 数据集合
      list: [],
      // 选中行key集合
      selectedRowKeys: [],
      // 选中行集合
      selectedRows: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 选中主键集合
      ids: [],
      // 是否加载
      loading: false,
      // 数据总量
      total: 0,
      // 状态数据字典
      statusOptions: [],
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParam: {
        pageNum: 1,
        pageSize: 10,
        cityName: undefined,
        cityLevel: undefined,
        linesLevel: undefined
      },
      expandedKeys: [],
      columns: [
        {
          title: '主键',
          dataIndex: 'cityId',
          ellipsis: true,
          align: 'center'
        },
        {
          title: '城市名称',
          dataIndex: 'cityName',
          scopedSlots: { customRender: 'cityName' },
          align: 'center'
        },
        {
          title: '城市级别',
          dataIndex: 'cityLevel',
          scopedSlots: { customRender: 'cityLevel' },
          align: 'center'
        },
        {
          title: '城市线级',
          dataIndex: 'linesLevel',
          scopedSlots: { customRender: 'linesLevel' },
          align: 'center'
        },
        {
          title: '常住人口总数(万人)',
          dataIndex: 'totalResidentPopulation',
          ellipsis: true,
          align: 'center'
        },
        {
          title: '新增人口总数(万人)',
          dataIndex: 'newResidentPopulation',
          ellipsis: true,
          align: 'center'
        },
        {
          title: '户籍人口总数(万人)',
          dataIndex: 'totalRegisteredPopulation',
          ellipsis: true,
          align: 'center'
        },
        {
          title: '家庭户数(万户)',
          dataIndex: 'familyNumber',
          ellipsis: true,
          align: 'center'
        },
        {
          title: '城市面积(km²)',
          dataIndex: 'cityArea',
          ellipsis: true,
          align: 'center'
        },
        {
          title: '操作',
          dataIndex: 'operation',
          scopedSlots: { customRender: 'operation' },
          align: 'center'
        }
      ]
    }
  },
  filters: {

  },
  created () {
    this.getDicts('city_level').then(response => {
      this.cityLevelOptions = response.data
    })
    this.getDicts('lines_level').then(response => {
      this.linesLevelOptions = response.data
    })
    this.getList()
  },
  computed: {},
  watch: {},
  methods: {
    /** 权限判断 */
    checkPermi,
    /** 查询字典列表 */
    getList () {
      this.loading = true
      listCity(this.queryParam).then(response => {
          this.list = response.rows
          this.total = response.total
          this.loading = false
        }
      )
    },
    /** 城市级别字典翻译 */
    cityLevelFormat (val) {
      return this.selectDictLabel(this.cityLevelOptions, val)
    },
    /** 城市线级字典翻译 */
    linesLevelFormat (val) {
      return this.selectDictLabel(this.linesLevelOptions, val)
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParam.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.dateRange = []
      this.queryParam = {
        pageNum: 1,
        pageSize: 10,
        cityName: undefined,
        cityLevel: undefined,
        linesLevel: undefined
      }
      this.handleQuery()
    },
    /** 分页改变操作 */
    onShowSizeChange (current, pageSize) {
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    /** 页码改变操作 */
    changeSize (current, pageSize) {
      this.queryParam.pageNum = current
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    /** 表格选中操作 */
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
      this.ids = this.selectedRows.map(item => item.dictId)
      this.single = selectedRowKeys.length !== 1
      this.multiple = !selectedRowKeys.length
    },
    /** 搜索展开/收起操作 */
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const that = this
      const cityIds = row.cityId || this.ids
      this.$confirm({
        title: '删除提示',
        content: row.cityName ? '确定要删除' + row.cityName + '的数据吗？' : '确定要删除选中的数据吗？',
        onOk () {
          return delCity(cityIds)
            .then(() => {
              that.onSelectChange([], [])
              that.getList()
              that.$message.success(
                '删除成功',
                3
              )
            })
        },
        onCancel () {
        }
      })
    },
    /** 导出按钮操作 */
    handleExport () {
      const that = this
      this.$confirm({
        title: '导出提示',
        content: '此操作将导出当前条件下所有数据而非选中数据',
        onOk () {
          that.download('platform/city/export', {
            ...that.queryParam
          }, `city_${new Date().getTime()}.xlsx`)
        },
        onCancel () {
        }
      })
    },
    /** 子表展开操作 */
    onExpandCurrent (expandedKeys, row) {
      if (expandedKeys) {
        this.expandedKeys = [row.cityId]
      } else {
        this.expandedKeys = []
      }
    }
  }
}
</script>
